package geektime.task.race;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;




/**
 * 	计算随机数工具类
 * @author
 *
 */
public class SimpleDivideCompute implements ComputeRunnable {

	SimpleShareData ssd;
	int size;
	int offset;
	public SimpleDivideCompute(SimpleShareData ssd, int size, int offset) {
		this.ssd = ssd;
		this.size = size;
		this.offset = offset;
	}
	
	@Override
	public void run() {
		go();

	}

	@Override
	public void go() {
		System.out.println("开始计算随机数: " + size + " " + offset);
		
		List<Integer> alist=(List<Integer>) ssd.getScore().subList(size*offset, size*((offset+1)));
		
		Collections.sort(alist,new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2.compareTo(o1);
			}
		});
		
		ssd.addExchanges(alist.subList(0, SimpleShareData.BUFSIZE * size / SimpleShareData.COUNT));
		ssd.getCompSig().countDown();
		//System.out.println("计算随机数完毕: " + size + " " + SimpleShareData.BUFSIZE * size / SimpleShareData.COUNT);
		
	}
}
